# -*- coding: utf-8 -*-
import pymysql
import re
#连接数据库
try:
    conn = pymysql.Connection(host="192.168.0.113",port=3306,user='coname',password='xZS6p4LDcKCwn4Yb',db="co",charset="utf8")
    cursor = conn.cursor()
    print('连接数据库成功!')
except Exception as e:
    print("连接数据库失败!>>>",e)
#获取水果数据源
try:
    cursor.execute('select content,srname from co_fruit ')#where srname ="桑葚"
    resutls = cursor.fetchall()
except Exception as e:
    print('查询数据失败!>>>',e)
#没有编辑的文字匹配函数
def getPat(texts,patss):
    # pat = '(\d+)'+patss
    # #获取编号
    # num1 = re.findall(pat, texts)[0]
    # num2 = str(int(num1)+1)
    #截取到下一个中括号
    pats = patss+'(.*?)\['
    ss2 = re.findall(pats, texts)
    #截取目标结果
    cont=''
    for ss2_ in ss2:
        cont=cont+ss2_
    return cont
res2=''
#获取清洗后的字段
try:
    for resutl in resutls:
        res1=resutl[1] 
        res0=resutl[0]
        # print(res0)
        if "栽培技术编辑" in res0:
            pat="栽培技术编辑(.*?)编辑"
        elif "种植技术编辑" in res0:
            pat="种植技术编辑(.*?)编辑"
        elif "植物栽培编辑" in res0:
            pat="植物栽培编辑(.*?)编辑"
        elif "栽植技术编辑" in res0:
            pat="栽植技术编辑(.*?)编辑"
        elif "生产栽培编辑" in res0:
            pat="生产栽培编辑(.*?)编辑"
        elif '栽培技术' in res0:
            pat='栽培技术(.*?)\['
        else:
            print('更新<<<'+res1+'>>>种植技术失败')
            continue
        res0s = re.findall(pat, res0)
        cont=''
        if len(res0s)>0:
            print(len(res0s))
            if len(res0s)==1:
                cont=cont+res0s[0]
                cursor.execute('update co_fruit set plantingTechnology="{}" where srname="{}"'.format(cont,res1))
                conn.commit()
                print("更新<<<"+res1+">>>种植技术成功")
            else:
                res0s1=res0s[1:0]
                for conts in res0s1:
                    cont=cont+conts
                    cursor.execute('update co_fruit set plantingTechnology="{}" where srname="{}"'.format(cont,res1))
                    conn.commit()
                    print("更新<<<"+res1+">>>种植技术成功")
        else:
            continue
        # if res2 !='':
        #     cursor.execute('update co_fruit set plantingTechnology="{}" where srname="{}"'.format(res2,res1))
        #     conn.commit()
        #     print("更新<<<"+res1+">>>种植技术成功")
except Exception as e:
    print('更新数据失败!>>>'+res1,e)
#关闭数据库
try:
    cursor.close()
    conn.close()
    print('关闭数据库成功!')
except Exception as e:
    print('连接数据库失败>>>',e)